package gang.com.downloader;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements OnClickListener {
    private Button mButton;
    private DownloadManager mDownloadManager;
    //new一个抽象类的对象和继承某个抽象类是同等概念
    private DataWatcher watcher = new DataWatcher() {
        @Override
        public void notifyUpdate(DownloadEntry data) {
            Trace.d(data.toString());
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.main_download_btn);
        mButton.setOnClickListener(this);
        mDownloadManager = DownloadManager.getmIntance(this);
    }

    @Override
    public void onClick(View view) {
        DownloadEntry entry = new DownloadEntry();
        entry.name = "test.jpg";
        entry.url = "http://static.firefoxchina.cn/img/201711/12_59fabfe30f3d80.jpg";
        entry.id = "1";
        mDownloadManager.add(this, entry);


    }

    @Override
    protected void onResume() {
        super.onResume();
        mDownloadManager.addObserver(watcher);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mDownloadManager.removeObserver(watcher);
    }
}
